package org.weso.acota.utils;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.tartarus.snowball.ext.SpanishStemmer;


@SuppressWarnings("deprecation")
public class SpanishStemFilter extends TokenFilter {

	private SpanishStemmer stemmer;   
    @SuppressWarnings("unused")
	private Token token = null;  
	private TermAttribute termAttr; 
    @SuppressWarnings("unused")
	private TypeAttribute typeAttr;
    protected static Logger logger = Logger.getLogger(SpanishStemFilter.class);
	
	
	protected SpanishStemFilter(TokenStream input) {
		super(input);
		stemmer = new SpanishStemmer();
		termAttr = (TermAttribute) addAttribute(TermAttribute.class); 
		typeAttr = (TypeAttribute) addAttribute(TypeAttribute.class);
	}
	
	public void setStemmer(SpanishStemmer stemmer) {   
        if ( stemmer != null ) {   
            this.stemmer = stemmer;   
        }   
    }

	@Override
	public boolean incrementToken() throws IOException {
		logger.debug("Set stemmer of each token");
		if (!input.incrementToken()){
			return false;
		}else{
			stemmer.setCurrent(termAttr.term()); 
			stemmer.stem();
			String encoded = stemmer.getCurrent();
			termAttr.setTermBuffer(encoded); 
			return true;
		}		
	}   

}
